package com.itheima.controller;


import com.itheima.pojo.Result;
import com.itheima.utils.AilOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class FileUploadController {


    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws Exception {
        //把文件内容上传到本地磁盘
        String originalFileName=file.getOriginalFilename();

        //利用UUID确保文件名字是唯一的，防止文件覆盖
        String filename= UUID.randomUUID()+originalFileName.substring(originalFileName.lastIndexOf("."));

        //file.transferTo(new File("D:\\springbootFile\\"+filename));


       String url= AilOssUtil.uploadFile(filename,file.getInputStream());


        return Result.success(url);

    }





}
